"""
文件操作模块有以下功能(写在类里面的函数)：
读取文件内容
写入内容到文件中
复制文件
删除文件
文件改名
"""


class FileOperation:

    @staticmethod
    def read(filename):
        f = open(f'{filename}', 'r', encoding='utf8')
        content = f.read()
        f.close()
        return content

    @staticmethod
    def write(filename, content):
        f = open(f'{filename}', 'a', encoding='utf8')
        f.write(content)
        f.close()
        print('写入成功')

    @staticmethod
    def copy(old_filename):
        import os
        filename, extend = os.path.splitext(old_filename)
        new_filename = filename + '-副本' + extend
        old_filename = open(old_filename, 'rb')
        new_filename = open(new_filename, 'ab')

        while True:
            content = old_filename.read(1024)
            if not content:
                break
            new_filename.write(content)

        old_filename.close()
        new_filename.close()

    @staticmethod
    def delete(filename):
        import os
        os.remove(filename)
        print('删除成功')

    @staticmethod
    def rename(filename, new_filename):
        import os
        os.rename(filename, new_filename)
        print('修改成功')


FileOperation.write('demo1.txt', '987654321')
print(FileOperation.read('demo1.txt'))
FileOperation.copy('demo1.txt')
FileOperation.rename('demo1-副本.txt', 'demo2.txt')
FileOperation.delete('demo2.txt')